﻿namespace Congstarrechnung
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using System.Xml.Serialization;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int rowElems;
            Invoice inv = new Invoice();
            Entry entry;
            string[] elems = textBox1.Text.Split(new []{'\n'});

            for (int i = 0; i < elems.Length; i+= rowElems)
            {
                entry = new Entry();

                entry.Date = DateTime.ParseExact(
                    elems[i].Replace("\r", " ") + elems[i + 2].Replace("\r", ""), 
                    "dd.MM.yy HH:mm:ss", 
                    null);
                entry.PhoneNumber = elems[i + 1].Replace("\r", " ");

                string[] s = elems[i + 3].Split(new[] {':'});
                entry.LengthInSeconds = int.Parse(s[0])*3600 + int.Parse(s[1])*60 + int.Parse(s[2]);
                entry.Einheiten = (int)(entry.LengthInSeconds / 60.0f + 1);
                entry.Provider = elems[i + 4];
                float cents = float.Parse(elems[i + 5]) * 100;
                // float cents = float.Parse(elems[i + 5].Replace(',', '.')) * 100;
                entry.Charges = cents;

                if (entry.Charges != 0)
                {
                    rowElems = 6;
                    entry.Type = "None";
                }
                else
                {
                    rowElems = 7;
                    entry.Type = elems[i + 6];
                }

                inv.Add(entry);
            }


            inv.Finalize();
            FileStream fs = new FileStream(
                Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\congstar.xml",                               FileMode.Create);
            XmlSerializer serializer = new XmlSerializer(typeof(Invoice));
            serializer.Serialize(fs, inv);
            fs.Close();

            MessageBox.Show(@"congstar.xml auf dem Desktop gespeichert.");
        }
    }
}
